package com.aaa.cars.service.impl;

import com.aaa.cars.dao.BrandDao;
import com.aaa.cars.entity.Brand;
import com.aaa.cars.entity.Car;
import com.aaa.cars.service.BrandService;
import com.aaa.common.constants.ExceptionConstant;
import com.aaa.common.constants.ReturnStatus;
import com.aaa.common.util.CustomException;
import com.aaa.common.util.Result;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * (Brand)表服务实现类
 *
 * @author makejava
 * @since 2020-12-04 18:36:48
 */
@Service("brandService")
public class BrandServiceImpl implements BrandService {
    @Resource
    private BrandDao brandDao;

    /**
     * 通过ID查询单条数据
     *
     * @param id 主键
     * @return 实例对象
     */
    @Override
    public Brand queryById(Integer id) {
        return this.brandDao.queryById(id);
    }

    /**
     * 通过实体作为筛选条件查询
     *
     * @param pageNo
     * @param pageSize
     * @param brand    实例对象
     * @return 对象列表
     */
    @Override
    public Result queryAll(int pageNo, int pageSize, Brand brand) {
        if(pageNo==0||pageSize==0){
            throw new CustomException(ExceptionConstant.INVALID_ARGUMENT.getErrorCode(),
                    ExceptionConstant.INVALID_ARGUMENT.getErrorMessage());
        }
        //分页
        PageHelper.startPage(pageNo,pageSize);
        List<Brand> brandList = brandDao.queryAll(brand);
        if (brandList != null && brandList.size() > 0) {
            for (Brand brand1 : brandList) {
                Integer factoryStatus = brand1.getFactoryStatus();
                if (factoryStatus == 1) {
                    brand1.setFactoryName("是");
                } else {
                    brand1.setFactoryName("不是");
                }
                Integer showStatus = brand1.getShowStatus();
                if (showStatus == 1) {
                    brand1.setShwoName("是");
                } else {
                    brand1.setShwoName("否");
                }
            }
        }
        PageInfo<Brand> pageInfo = new PageInfo<Brand>(brandList);
        return new Result(ReturnStatus.SUCCESS.getReturnCode(),
                ReturnStatus.SUCCESS.getReturnMsg(), pageInfo);
    }

    /**
     * 查询多条数据
     *
     * @param offset 查询起始位置
     * @param limit  查询条数
     * @return 对象列表
     */
    @Override
    public List<Brand> queryAllByLimit(int offset, int limit) {
        return this.brandDao.queryAllByLimit(offset, limit);
    }

    /**
     * 新增数据
     *
     * @param brand 实例对象
     * @return 实例对象
     */
    @Override
    public Brand insert(Brand brand) {
        if (brand.getFactoryName().equals("是")){
            brand.setFactoryStatus(1);
        } else {
            brand.setFactoryStatus(0);
        }
        if (brand.getShwoName().equals("是")) {
            brand.setShowStatus(1);
        } else {
            brand.setShowStatus(0);
        }
        brand.setAddtime(new Date());
        this.brandDao.update(brand);
        this.brandDao.insert(brand);
        return brand;
    }

    /**
     * 修改数据
     *
     * @param brand 实例对象
     * @return 实例对象
     */
    @Override
    public Brand update(Brand brand) {
        if (brand.getFactoryName().equals("是")){
            brand.setFactoryStatus(1);
        } else {
            brand.setFactoryStatus(0);
        }
        if (brand.getShwoName().equals("是")) {
            brand.setShowStatus(1);
        } else {
            brand.setShowStatus(0);
        }
        brand.setAddtime(new Date());
        this.brandDao.update(brand);
        return this.queryById(brand.getId());
    }

    /**
     * 通过主键删除数据
     *
     * @param id 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(Integer id) {
        return this.brandDao.deleteById(id) > 0;
    }
}